var ws = {
	conn: null,
	wId: null,
	reConnectInterval: null,
	status: 0,  // 1 已连接，0 未连接 -1 服务器中断，-2 主动中断
	open(u,id,cb) {
		let $ = this
		//if ($.conn) $.conn.close()
		$.conn = new WebSocket(u)
		$.onError($,cb)
		$.conn.onopen = function (e) { 
	    	$.wId = id
	    	$.status = 1
	    	let obj = { type: 'name', data: id}
	    	$.conn.send(JSON.stringify(obj))
	    	cb(e)
	  	}
	},
	onError($,cb) {
		$.conn.onerror = function (e) { 
  		// console.log(e)
  		$.status = -1
  		cb(e)
  	}
	},
	close(cb) {
		let $ = this
		$.conn.close()
		clearInterval($.heartInterval)
    	$.onError($,cb)
	},
	onClose(cb) {
		let $ = this
		$.conn.onclose = function(e) {
    		// console.log(e)
	    	cb(e)
	    }
	},
	send(t,d,cb) {
		let $ = this
		let obj = { type: t, data: d}
		console.log(obj)
    	$.conn.send(JSON.stringify(obj))
    	$.onError($,cb)
    	cb($.status)
	},
	onMsg(cb) {
		let $ = this
		$.conn.onmessage = function(e) { 
			// console.log(e)
			cb(e)
			// let data = JSON.parse(e.data)
			// if (data.type != 'heart') cb(e)
		}            
	},
	onHeart(delay,cb) {
		let $ = this
		clearInterval($.heartInterval)
		let obj = { type: 'heart', data: 'ping'}
		$.heartInterval = setInterval(function() {
			if ($.status == 1) {
				$.conn.send(JSON.stringify(obj))
			} else {
				cb($.status)
			}
		},delay)
	}
}